/**
 * Created by Administrator on 2017/4/7.
 */
var av_money = 0;
$(function(){
    var key = getCookie('key');
    if(!key){
        window.location.href = WapSiteUrl+'/tmpl/member/login.html';
    }
    var username = '';
    if(key){
        $.ajax({
            type:'post',
            url:ApiUrl+"/index.php?act=member_index&op=get_predeposit",
            data:{key:key,form:f},
            dataType:'json',
            success:function(result){
                av_money = parseInt(result['datas']['available_predeposit']);
                if (result['code']==200){
                    $('#money').html(result['datas']['available_predeposit']);
                }
            }
        });
    } else {
        window.location.href = "login.html";
    }

    $("#moneynum").on('input',function(){
          if($(this).val()>av_money){
                $(this).css('borderColor',"#ff6600");
          }else{
                $(this).css('borderColor',"#ccc");
          }
    })

    $("#doWithDrawal").on('click',function(){
        var moneynum =$("#moneynum").val();
        if (moneynum==''){
            creatLayout("请填写提现金额");
            return;
        }
        var re = /^[0-9]*[0-9]$/i;
        if(!re.test(moneynum)){
            creatLayout('提现金额必须为数字');
            return;
        }
        if (moneynum%100!=0){
            creatLayout("提现金额必须为100的整数倍");
            return;
        }
        var mymoney = parseInt($("#money").text());
        var tixian = parseInt(moneynum);
        if (mymoney <  tixian){
            creatLayout("提现金额不足！");
            return;
        }
        data = {key:key,moneynum:moneynum,op:f}
        $.sDialog({
            skin:"block",
            content:'确认提现吗？',
            okBtn:true,
            cancelBtn:true,
            okFn: function() {
                doWithDrawal(data);
            }
        });
    });

    // 提现
    function doWithDrawal(data){
        $.ajax({
             type:"POST",
             url:ApiUrl+"/index.php?act=member_index&op=do_withdrawal",
             data:data,
             dataType:"json",
            success:function(result){
                var res = result.datas;
                   if (res['code']==200){
                       creatLayout(res['msg']);
                   }else{
                        if (res['code']==401){
                            $.sDialog({
                                skin:"block",
                                content:'还未绑卡,是否绑卡？',
                                okBtn:true,
                                cancelBtn:true,
                                okFn: function() {
                                      window.location.href = WapSiteUrl+"/tmpl/member/edit_card.html";
                                }
                            });
                            return;
                        }
                        creatLayout(res['msg'])
                   }
             }
        })
    }
});